﻿using HDJ.Framework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace HDJ.Framework.Core
{
    public class LogicSystemEditorWindow:EditorWindow
    {
        public static LogicSystemEditorWindow Instance;
        [MenuItem("Tools/TestLogicWin")]
        public static void Test()
        {
            ShowWindow("TestLogicWin", "LogicObjects/MapLogicObjects/");
        }
        public static void ShowWindow(string logicDataName, string savePathDic)
        {
            Instance= GetWindow<LogicSystemEditorWindow>();
            Instance.logicDataName = logicDataName;
            Instance.savePathDic = savePathDic;
            Instance.Init();

        }
        private  string logicDataName = "";
        public  string savePathDic = "";

        public  LogicObjectContainer data;
        /// <summary>
        /// 设置内部变量的值
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public  void SetInternalValue(string name, object value)
        {
            BaseValue bv = data.GetBaseValue(name);
            if (bv != null)
            {
                bv.SetValue(value);
            }
        }
        /// <summary>
        /// 获取内部变量的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public  object GetInternalValue(string name)
        {
            BaseValue bv = data.GetBaseValue(name);
            if (bv != null)
                return bv.GetValue();
            else
            {
                Debug.LogError("不存在内部变量：" + name);
                return null;
            }
        }
        SM_EditorController<LogicObject, StateTransitionDataBase> drawWin;
         void Init()
        {
            data = LogicObjectDataController.GetDataFromFile(logicDataName);

            drawWin= SM_EditorController<LogicObject, StateTransitionDataBase>.Init(data, Instance);
            drawWin.OnDrawLeftPartGUI = OnDrawLeftPartGUI;

            internalValueTypeList.Add(typeof(string).FullName);
            internalValueTypeList.Add(typeof(int).FullName);
            internalValueTypeList.Add(typeof(float).FullName);
            internalValueTypeList.Add(typeof(Vector2).FullName);
            internalValueTypeList.Add(typeof(Vector3).FullName);
            internalValueTypeList.Add(typeof(bool).FullName);
        }
        private void OnGUI()
        {
            if (drawWin == null)
                return;

            drawWin.OnGUI();
        }
        private void OnDestroy()
        {
            if (drawWin == null)
                return;

            drawWin.OnDestroy();
        }

        List<string> internalValueTypeList = new List<string>();

        public  int toolbarOption = 0;
         public string[] toolbarTexts = { "内部变量", "备份组件" };
         void OnDrawLeftPartGUI()
        {
            //titleContent.text = "逻辑系统编辑器";
            GUILayout.BeginHorizontal("box");
            GUILayout.FlexibleSpace();
            GUILayout.Label("Logic File Name：" + logicDataName);
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
            GUILayout.Space(2);

            toolbarOption = GUILayout.Toolbar(toolbarOption, toolbarTexts);
            switch (toolbarOption)
            {
                case 0:
                    InternalVariableGUI();
                    break;
                case 1:
                    LogicObjectBackUpEditor.Instance.BackupGUI();

                    break;

            }

            GUILayout.Space(7);
            if (GUILayout.Button("保存"))
            {
                SaveData(logicDataName, savePathDic, data);
              
                EditorUtility.DisplayDialog("提示", "保存成功", "OK");
            }

        }

         private Vector2 pos = Vector2.zero;
         private string internalValueName = "";
         private string internalValueType = "";
         void InternalVariableGUI()
        {
            GUIStyle style0 = "IN GameObjectHeader";
            style0.fontSize = 12;
            GUILayout.Label("新建内部变量", style0);
            //GUIStyle style = "flow overlay header lower left";

            GUILayout.BeginHorizontal("box");
            GUILayout.BeginVertical();
            internalValueName = EditorDrawGUIUtil.DrawBaseValue("名字", internalValueName).ToString();
            internalValueType = EditorDrawGUIUtil.DrawPopup("数据类型", internalValueType, internalValueTypeList);
            GUILayout.EndVertical();
            if (GUILayout.Button("+", GUILayout.Width(35)))
            {
                if (!string.IsNullOrEmpty(internalValueName) && data.GetBaseValue(internalValueName) == null)
                {
                    data.internalValueList.Add(new BaseValue(internalValueName, LogicSystemEditorTools.GetDefultValueByTypeName(internalValueType)));
                }
                else
                {
                    EditorUtility.DisplayDialog("错误", "名字不能重复！", "OK");
                }
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(10);
            pos = GUILayout.BeginScrollView(pos, true, true);
            GUILayout.BeginVertical("box");


            foreach (BaseValue vt in data.internalValueList)
            {
                GUILayout.BeginHorizontal("box");
                GUILayout.BeginVertical();
                vt.name = EditorDrawGUIUtil.DrawBaseValue("名字(" + vt.typeName + ")", vt.name).ToString();
                object v = EditorDrawGUIUtil.DrawBaseValue("Value", vt.GetValue());
                vt.SetValue(v);
                GUILayout.EndVertical();
                if (GUILayout.Button("-", GUILayout.Width(25)))
                {
                    data.internalValueList.Remove(vt);
                    return;
                }
                GUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();
            GUILayout.EndVertical();
        }
        public  void SaveData(string dataName, string savePathDic, LogicObjectContainer data)
        {
           
            string path = PathUtils.GetSpecialPath(savePathDic + dataName + ".txt", SpecialPathType.Resources);;
            string content = JsonUtils.ToJson(data);

            FileUtils.CreateTextFile(path, content);
            AssetDatabase.Refresh();
        }

    }
}
